package com.byb.server.service.impl;

import com.byb.server.service.TypesOfInsuranceService;
import com.byb.server.domain.TypesOfInsurance;
import com.byb.server.repository.TypesOfInsuranceRepository;
import com.byb.server.repository.search.TypesOfInsuranceSearchRepository;
import com.byb.server.service.dto.TypesOfInsuranceDTO;
import com.byb.server.service.mapper.TypesOfInsuranceMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import static org.elasticsearch.index.query.QueryBuilders.*;

/**
 * Service Implementation for managing TypesOfInsurance.
 */
@Service
@Transactional
public class TypesOfInsuranceServiceImpl implements TypesOfInsuranceService{

    private final Logger log = LoggerFactory.getLogger(TypesOfInsuranceServiceImpl.class);

    private final TypesOfInsuranceRepository typesOfInsuranceRepository;

    private final TypesOfInsuranceMapper typesOfInsuranceMapper;

    private final TypesOfInsuranceSearchRepository typesOfInsuranceSearchRepository;
    public TypesOfInsuranceServiceImpl(TypesOfInsuranceRepository typesOfInsuranceRepository, TypesOfInsuranceMapper typesOfInsuranceMapper, TypesOfInsuranceSearchRepository typesOfInsuranceSearchRepository) {
        this.typesOfInsuranceRepository = typesOfInsuranceRepository;
        this.typesOfInsuranceMapper = typesOfInsuranceMapper;
        this.typesOfInsuranceSearchRepository = typesOfInsuranceSearchRepository;
    }

    /**
     * Save a typesOfInsurance.
     *
     * @param typesOfInsuranceDTO the entity to save
     * @return the persisted entity
     */
    @Override
    public TypesOfInsuranceDTO save(TypesOfInsuranceDTO typesOfInsuranceDTO) {
        log.debug("Request to save TypesOfInsurance : {}", typesOfInsuranceDTO);
        TypesOfInsurance typesOfInsurance = typesOfInsuranceMapper.toEntity(typesOfInsuranceDTO);
        typesOfInsurance = typesOfInsuranceRepository.save(typesOfInsurance);
        TypesOfInsuranceDTO result = typesOfInsuranceMapper.toDto(typesOfInsurance);
        //typesOfInsuranceSearchRepository.save(typesOfInsurance);
        return result;
    }

    /**
     *  Get all the typesOfInsurances.
     *
     *  @param pageable the pagination information
     *  @return the list of entities
     */
    @Override
    @Transactional(readOnly = true)
    public Page<TypesOfInsuranceDTO> findAll(Pageable pageable) {
        log.debug("Request to get all TypesOfInsurances");
        return typesOfInsuranceRepository.findAll(pageable)
            .map(typesOfInsuranceMapper::toDto);
    }

    /**
     *  Get one typesOfInsurance by id.
     *
     *  @param id the id of the entity
     *  @return the entity
     */
    @Override
    @Transactional(readOnly = true)
    public TypesOfInsuranceDTO findOne(Long id) {
        log.debug("Request to get TypesOfInsurance : {}", id);
        TypesOfInsurance typesOfInsurance = typesOfInsuranceRepository.findOne(id);
        return typesOfInsuranceMapper.toDto(typesOfInsurance);
    }

    /**
     *  Delete the  typesOfInsurance by id.
     *
     *  @param id the id of the entity
     */
    @Override
    public void delete(Long id) {
        log.debug("Request to delete TypesOfInsurance : {}", id);
        typesOfInsuranceRepository.delete(id);
        //typesOfInsuranceSearchRepository.delete(id);
    }

    /**
     * Search for the typesOfInsurance corresponding to the query.
     *
     *  @param query the query of the search
     *  @param pageable the pagination information
     *  @return the list of entities
     */
    @Override
    @Transactional(readOnly = true)
    public Page<TypesOfInsuranceDTO> search(String query, Pageable pageable) {
        log.debug("Request to search for a page of TypesOfInsurances for query {}", query);
        Page<TypesOfInsurance> result = typesOfInsuranceSearchRepository.search(queryStringQuery(query), pageable);
        return result.map(typesOfInsuranceMapper::toDto);
    }
}
